//
//  YYUserCarDetailVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/11/19.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYUserCarDetailVC.h"
#import "YYSubscribeView.h"

@interface YYUserCarDetailVC ()<SDCycleScrollViewDelegate,UIWebViewDelegate,UIScrollViewDelegate>
@property (nonatomic,strong)NSDictionary * dataDic;
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;

@end

@implementation YYUserCarDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.wevView.opaque = NO; // 去除底部黑块
    self.wevView.scrollView.showsHorizontalScrollIndicator = NO;
    self.lbTag.layer.borderWidth = 0.75;
    self.lbTag.layer.borderColor = [UIColor colorWithHexString:@"fb2a2a"].CGColor;
    self.lbTag.layer.cornerRadius = 2;
    self.lbTag.layer.masksToBounds = YES;
    self.segmentView.sectionTitles = @[@"基本信息", @"车辆详情"];
    self.segmentView.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
    self.segmentView.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : DEF_COLOR_MAIN,NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
    self.segmentView.selectionIndicatorColor = DEF_COLOR_MAIN;
    self.segmentView.selectionIndicatorHeight = 2;
    self.segmentView.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.segmentView.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    __weak typeof(self) weakSelf = self;
    [weakSelf.segmentView setIndexChangeBlock:^(NSInteger index) {
        if (index == 0) {
            [weakSelf.scrollView setContentOffset:CGPointMake(0, CGRectGetMaxY(self.imgView.frame)-48) animated:YES];
        }else{
         [weakSelf.scrollView setContentOffset:CGPointMake(0, CGRectGetMaxY(self.infoView.frame)-48) animated:YES];
        }
    }];
    
    [self getData];
}


-(void)getData{
    [RequestOperationManager useredCarDetail_id:self.carId success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.dataDic = [result[@"data"] copy];
            [self reloadData];
        }
        
    } failture:^(NSError *error) {
        
    }];
}

-(void)reloadData{
    self.lbName.text = [NSString stringWithFormat:@"%@ %@",self.dataDic[@"brand"],self.dataDic[@"vehicle_type"]];
    self.lbSeller.text = self.dataDic[@"seller_name"];
    self.lbAdress.text = self.dataDic[@"seller_address"];
    if ([self.dataDic[@"benefit_money"] length]>0) {
        self.lbPrice.text = [NSString stringWithFormat:@"%@",self.dataDic[@"benefit_money"]];
        self.OPLine.hidden = NO;
        self.lbOldPrice.hidden = NO;
        self.lbOldPrice.text =[NSString stringWithFormat:@"新车原价￥%@",self.dataDic[@"money"]];
    }else{
        self.OPLine.hidden = YES;
        self.lbOldPrice.hidden = YES;
        self.lbPrice.text = [NSString stringWithFormat:@"%@",self.dataDic[@"money"]];
    }
    
    NSArray * bannerArr = self.dataDic[@"vehicle_img"];
    NSMutableArray * imageArr = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray * titleArr = [NSMutableArray arrayWithCapacity:0];
    for (int i =0; i<bannerArr.count; i++) {
        [imageArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,bannerArr[i]]];
        [titleArr addObject:[NSString stringWithFormat:@"%i/%lu",i+1,(unsigned long)bannerArr.count]];
    }
    self.adCycleScrollView.titlesGroup = titleArr;
    self.adCycleScrollView.imageURLStringsGroup =imageArr;
    self.lbChange.text = [NSString stringWithFormat:@"%@过户",self.dataDic[@"transfer"]];
    
    self.label1.text = self.dataDic[@"type_id"];
    self.lb2.text = self.dataDic[@"oncard"];
    self.lb3.text = self.dataDic[@"location"];
    self.lb4.text = self.dataDic[@"change"];
    self.lb5.text = self.dataDic[@"transfer"];
    self.lb6.text = self.dataDic[@"pay"];
    self.lb7.text = self.dataDic[@"monthly"];
    self.lb8.text = self.dataDic[@"check"];
    self.lb9.text = self.dataDic[@"move"];
    
    [self.rollImg1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.dataDic[@"roll1"]]]];
    [self.rollImg2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.dataDic[@"roll2"]]]];
    [self.rollImg3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.dataDic[@"roll3"]]]];
    [self.wevView loadHTMLString:self.dataDic[@"content"] baseURL:nil];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contentY = scrollView.contentOffset.y;
    if (contentY>CGRectGetMaxY(self.imgView.frame)-50) {
        self.segmentView.hidden = NO;
    }else{
         self.segmentView.hidden = YES;
    }
}
- (IBAction)call:(id)sender {
    NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",self.dataDic[@"seller_phone"]];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}
- (IBAction)lookCar:(id)sender {
    [YYSubscribeView sharedInstance].vehicle_id = self.carId;
    [YYSubscribeView sharedInstance].seller_id = self.dataDic[@"seller_id"];
    [YYSubscribeView show];
    
}

-(NSDictionary *)dataDic{
    if (!_dataDic) {
        _dataDic = [NSDictionary dictionary];
    }
    return _dataDic;
}

//UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    self.webViewHeight.constant = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
}


-(SDCycleScrollView *)adCycleScrollView{
    if (!_adCycleScrollView) {
        _adCycleScrollView= [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:[UIImage new]];
        _adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _adCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _adCycleScrollView.currentPageDotColor = [UIColor colorWithHexString:@"08c5ed"];
        _adCycleScrollView.pageDotColor = [UIColor colorWithHexString:@"bdbdbd"];
        _adCycleScrollView.pageControlDotSize = CGSizeMake(4, 4);  // pageControl小点的大小
        _adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bannerView addSubview:_adCycleScrollView];
        [_adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        
    }
    return _adCycleScrollView;
}
@end
